

import 'dart:ui';

import 'package:alip_app/AppStates/NetworkApi.dart';
import 'package:alip_app/Entities/UILayoutResponse.dart';
import 'package:alip_app/StateManagement/ModelProvider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'TopicsPage.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
  PageController _tabController;

  @override
  Widget build(BuildContext context) {
    Widget buildingWidget;
    buildingWidget = FutureBuilder<UILayoutResponse>(
      future: ModelContainer.of<NetworkApi>(context).model.api.getUILayouts(),
      builder: (context, snapshot) {
        if (snapshot.hasError) return Center(child: Text("An error occurred while loading data from server, here is the error message:\n${snapshot.error}"),);
        if (snapshot.hasData) {
          final homePageModule = snapshot.data.moduleList.where((element) => element.id == 3).first;
          _tabController ??= PageController();

          var pageIndex = 0;
          return Stack(
            children: <Widget>[
              Positioned.fill(child: PageView(
                physics: NeverScrollableScrollPhysics(),
                controller: _tabController,
                children: homePageModule.componentList.map((element) => _buildPage(element, pageIndex++)).toList(),
              )),
              Positioned(
                top: 90.0,
                left: 0.0,
                right: 0.0,
                height: 50.0,
                child: Center(
                  child: Container(
                    constraints: BoxConstraints(
                      maxWidth: 650.0,
                      minWidth: 300.0
                    ),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(5.0),
                      child: BackdropFilter(
                        filter: ImageFilter.blur(sigmaY: 20.0, sigmaX: 20.0),
                        child: Padding(
                          padding: EdgeInsets.symmetric(vertical: 10.0),
                          child: AnimatedBuilder(
                            animation: _tabController,
                            builder: (context, child) => CupertinoSegmentedControl(
                              unselectedColor: Theme.of(context).scaffoldBackgroundColor,
                              groupValue: (_tabController.page ?? 0.0).round(),
                              children: homePageModule.componentList.map((e) => Text(e.title)).toList().asMap(),
                              onValueChanged: (value) {
                                this._tabController.animateToPage(value, duration: Duration(seconds: 1), curve: Curves.elasticOut);
                              },
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          );
        }
        return Center(child: CircularProgressIndicator(),);
      },
    );
    return buildingWidget;
  }

  Widget _buildPage(ComponentList element, int pageIndex) {
    return TopicsPage(paddingHeight: 120.0, boardId: element.extParams.forumId, filterType: element.extParams.filter, orderBy: element.extParams.orderby, pageIndex: pageIndex,);
  }
}